package com.sakiko.javaConfig_02;

import com.alibaba.druid.pool.DruidDataSource;
import com.sakiko.ioc_02.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration // 把配置类加入 ioc容器中
@PropertySource("classpath:jdbc.properties")
public class JavaConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    //    <bean id="userService" class="com.sakiko.ioc_02.UserService"/>
    @Bean // ioc容器调用，并将返回值放入 ioc容器中
    public UserService userService() {
        return new UserService();
    }

    @Bean
    public DruidDataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(this.driver);
        druidDataSource.setUrl(this.url);
        druidDataSource.setUsername(this.username);
        druidDataSource.setPassword(this.password);
        return druidDataSource;
    }
}
